#pragma once

#include "Vectors.h"

#include <ctime>
#include <cassert>

inline int Rand()
{
	return rand();
}

inline int InitRandomSystem()
{
	static bool inited = false;
	static int seed = static_cast<unsigned int>(time(0));
	if(!inited)
	{
		inited = true;
		srand(seed);
		seed = Rand();
		srand(seed);
	}
	return seed;
}

inline int MultiRand(const size_t i_range, const size_t i_numRands)
{
	int sum=0;
	for(size_t i=0; i<i_numRands; ++i)
		sum += Rand()%i_range;
	return sum;
}

inline float RandFloat()
{
	return static_cast<float>(Rand()%(RAND_MAX) ) / static_cast<float>(RAND_MAX-1);
}

inline float RandFloatBetween(float i_min, float i_max)
{
	float delta = i_max - i_min;
	float r = RandFloat();
	return i_min + r * delta;
}

inline int RandIntBetween(int i_min, int i_max)
{
	const int delta = i_max - i_min;
	return i_min + Rand()%(1+delta);
}

inline int RandSelect(int i_first, int i_second)
{
	const int sum = i_first + i_second;
	assert(sum != 0);
	return Rand()%sum < i_first ? 0 : 1;
}

inline int RandSelect(int i_first, int i_second, int i_third)
{
	const int sum = i_first + i_second + i_third;
	assert(sum != 0);
	return Rand()%sum < i_third ? 2 : RandSelect(i_first,i_second);
}

inline int RandSelect(int i_first, int i_second, int i_third, int i_fourth)
{
	const int sum = i_first + i_second + i_third + i_fourth;
	assert(sum != 0);
	return Rand()%sum < i_fourth ? 3 : RandSelect(i_first, i_second, i_third);
}

inline int RandSelect(const IntVec& i_probs, const int i_sum)
{
	if(i_sum == 0)
		return 0;

	int min = 0;
	const int diceRoll = Rand()%i_sum;
	for(size_t i=0; i<i_probs.size(); ++i)
	{
		min += i_probs[i];
		if (diceRoll < min)
			return i;
	}
	assert(0);
	return 0;
}